package person.wend.javalearnexample.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

public class PropertiesExample {
    public static void main(String[] args) {
        // properties使用示例
        // 示例1 给properties 添加参数
        Properties properties = AddPropertiesTest();
        // 示例2 获取 properties 的参数
        getPropertiesTest(properties);
        // 示例3 编译 properties
        iteatorTest();
        // 示例4 读取properties 文件
        readPropertiesTest();
        // 示例5 写入properties 文件
        writePropertiesTest();
    }

    public static void readPropertiesTest() {
        Properties properties = new Properties();
        try {
            // 通过类加载器获取资源的URL
            URL resourceUrl = PropertiesExample.class.getClassLoader().getResource("config.properties");
            if (resourceUrl != null) {
                // 将URL转换为File对象（如果是在文件系统中，对于JAR包内资源这种方式有限制）
                File file = new File(resourceUrl.getPath());
                System.out.println("config.properties文件的路径（使用ClassLoader方式）：" + file.getAbsolutePath());
                FileInputStream fis = new FileInputStream(file);
                properties.load(fis);
                fis.close();
            } else {
                System.out.println("config.properties文件不存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("数据库名称: " + properties.getProperty("database.name"));
    }

    public static void writePropertiesTest() {
        Properties properties = new Properties();
        try {
            // 通过类加载器获取资源的URL
            URL resourceUrl = PropertiesExample.class.getClassLoader().getResource("config.properties");
            URL newResourceUrl = PropertiesExample.class.getClassLoader().getResource("newConfig.properties");
            if (resourceUrl != null) {
                // 将URL转换为File对象（如果是在文件系统中，对于JAR包内资源这种方式有限制）
                File file = new File(resourceUrl.getPath());
                System.out.println("config.properties文件的路径（使用ClassLoader方式）：" + file.getAbsolutePath());
                // 加载已有配置文件
                FileInputStream fis = new FileInputStream(file);
                properties.load(fis);
                fis.close();
                // 修改属性值
                properties.setProperty("user.password", "newPassword");
                // 保存到新文件
                FileOutputStream fos = new FileOutputStream(newResourceUrl.getPath());
                properties.store(fos, "Updated configuration");
                fos.close();
            } else {
                System.out.println("config.properties文件不存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Properties AddPropertiesTest() {
        Properties properties = new Properties();
        properties.setProperty("database.host", "localhost");
        properties.setProperty("database.port", "3306");
        return properties;
    }

    public static void getPropertiesTest(Properties properties) {
        System.out.println(properties.getProperty("database.host"));
        System.out.println(properties.getProperty("database.port"));
        String defaultValue = properties.getProperty("nonExistentKey", "defaultValue");
        System.out.println("不存在的键的值: " + properties.getProperty("database.user"));
        System.out.println("不存在的键的默认值: " + defaultValue);
    }

    public static void iteatorTest() {
        Properties properties = new Properties();
        properties.setProperty("key1", "value1");
        properties.setProperty("key2", "value2");
        // 使用keys()方法遍历
        Enumeration<?> propertyNames = properties.keys();
        while (propertyNames.hasMoreElements()) {
            String key = (String) propertyNames.nextElement();
            String value = properties.getProperty(key);
            System.out.println(key + " = " + value);
        }
        // 使用entrySet()方法遍历
        Set<?> entrySet = properties.entrySet();
        for (Object entryObj : entrySet) {
            java.util.Map.Entry<?, ?> entry = (java.util.Map.Entry<?, ?>) entryObj;
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }
    }
}
